package org.xa3apm.vote.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.security.DeclareRoles;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

import org.xa3apm.vote.hibernate.entities.Address;
import org.xa3apm.vote.hibernate.entities.Municipality;
import org.xa3apm.vote.hibernate.entities.Person;
import org.xa3apm.vote.hibernate.entities.Province;
import org.xa3apm.vote.hibernate.entities.Settlement;

@ManagedBean(name = "saver")
@SessionScoped
@DeclareRoles(value = { "DB_ADMIN" })
public class SaverBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9168805812765338877L;
	@EJB
	private EntityManagerBean entityManager;
	private List<Province> provinces;
	private List<Municipality> municipalities;
	private List<Settlement> settlements;
	private Person person = new Person();
	private Address address = new Address();
	private Province province;
	private Municipality municipality;
	private Settlement settlement;

	/**
	 * @return the province
	 */
	public final Province getProvince() {
		if(province==null){
			if (provinces!=null) {
				province = provinces.get(0);
			}
		}
		return province;
	}

	/**
	 * @param province
	 *            the province to set
	 */
	public final void setProvince(Province province) {
		this.province = province;
	}

	/**
	 * @return the municipality
	 */
	public final Municipality getMunicipality() {
		return municipality;
	}

	/**
	 * @param municipality
	 *            the municipality to set
	 */
	public final void setMunicipality(Municipality municipality) {
		this.municipality = municipality;
	}

	/**
	 * @return the settlement
	 */
	public final Settlement getSettlement() {
		return settlement;
	}

	/**
	 * @param settlement
	 *            the settlement to set
	 */
	public final void setSettlement(Settlement settlement) {
		this.settlement = settlement;
	}

	/**
	 * @return the provinces
	 */
	public final List<Province> getProvinces() {
		return (List<Province>) entityManager.getProvinceAll();
	}

	/**
	 * @return the municipalities
	 */
	public final List<Municipality> getMunicipalities() {
		if (municipalities==null) {
			if (municipalities!=null) {
				municipalities = (List<Municipality>) province.getMunicipalities();
			}
		}
		return municipalities;
	}

	/**
	 * @return the settlements
	 */
	public final List<Settlement> getSettlements() {
		return settlements;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public SaverBean() {
		// TODO Auto-generated constructor stub
	}
    public void valueChanged() {
    	if (province!=null) {
            municipalities.clear();
            municipalities = (List<Municipality>) province.getMunicipalities();
		} else if (provinces!=null){
			municipalities = (List<Municipality>) provinces.get(0).getMunicipalities();
		}
    }

    public void valueMunicipalityChanged(ValueChangeEvent event) {
        settlements.clear();
        if (null != event.getNewValue()) {
             settlements = entityManager.getSettlementMunicipalityAll(Long.parseLong(event.getNewValue().toString()));
        }
    }
}
